package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bwie.pojo.Brand;
import com.bwie.pojo.Category;
import com.bwie.pojo.CategoryBrand;
import com.bwie.service.BrandService;
import com.bwie.service.CategoryService;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: day0423-pxx
 * @ClassName CategoryController
 * @description:
 * @author: 段阳
 * @create: 2024-04-23 10:53
 **/
@RestController
@RequestMapping("item/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @GetMapping("/list")
    public ResponseEntity<List<Category>> getCategoryList(
            @RequestParam(value = "pid", defaultValue = "0") Long pid
    ) {
        List<Category> list = categoryService.list(
                new QueryWrapper<Category>().lambda().eq(Category::getParentId, pid)
        );
        return ResponseEntity.ok(list);
    }


    @PostMapping("/add")
    public R add(@RequestBody Category category) {
        Long parentId = category.getParentId();
        Category father = categoryService.getOne(
                new QueryWrapper<Category>().lambda().eq(Category::getId, parentId)
        );
        if (!father.getIsParent()) {
            UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
            wrapper.lambda().set(Category::getIsParent, 1).eq(Category::getId, father.getId());
            categoryService.update(wrapper);
        }

        return categoryService.addNewType(category);


    }


    @PostMapping("/edit")
    public R edit(
            @RequestBody Category category

    ) {

        Category category1 = categoryService.getOne(
                new QueryWrapper<Category>().lambda().eq(Category::getId, category.getId())
        );
        if (null == category1) {
            return R.error(500, "error");
        }
        if (category1.getName().equals(category.getName())) {
            return R.success("");
        }

        boolean edit = categoryService.updateById(category);
        return edit ? R.success("success") : R.error(500, "error");
    }

    //    @PostMapping("/edit/{id}/{name}")
//    public R edit(
//            @PathVariable(name = "id") Long id,
//            @PathVariable(name = "name") String name
//
//    ){
//
//        Category category1 = categoryService.getOne(
//                new QueryWrapper<Category>().lambda().eq(Category::getId,id)
//        );
//        if (null == category1){
//            return R.error(500,"error");
//        }
//        boolean update = categoryService.update(new UpdateWrapper<Category>().lambda().set(Category::getName, name).eq(Category::getId, category1.getId()));
////        boolean edit = categoryService.updateById(category);
//        return update?R.success("success"):R.error(500,"error");
//    }
    @PostMapping("/delete/{id}")
    public R delete(
            @PathVariable(name = "id") Long id

    ) {

        try {
            Category category = categoryService.getOne(
                    new QueryWrapper<Category>().lambda().eq(Category::getId, id)
            );
            if (null == category) {
                return R.error(500, "error");
            }
            if (!category.getIsParent()) {
                categoryService.removeById(category.getId());
                return R.success("success");
            }
            deleteCategory(category.getId());
            return R.success("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error(500, "error");
    }

    public void deleteCategory(Long id) {
        List<Category> list = categoryService.list(
                new QueryWrapper<Category>().lambda().eq(Category::getParentId, id)
        );
        for (Category category : list) {
            if (!category.getIsParent()) {
                categoryService.removeById(category.getId());
            } else {
                deleteCategory(category.getId());
            }
        }
        categoryService.removeById(id);


    }


    @GetMapping("/bid/{id}")
    public ResponseEntity<List<Category>> showById(
            @PathVariable(name = "id") Long id

    ) {
        Brand brand = brandService.getOne(
                new QueryWrapper<Brand>().lambda().eq(Brand::getId, id)
        );
        if (null == brand) {
            return null;
        }
        List<CategoryBrand> list = brandService.selectType(brand.getId());


        ArrayList<Category> categories = new ArrayList<>();
        for (CategoryBrand categoryBrand : list) {
            Category category = categoryService.getOne(new QueryWrapper<Category>().lambda().eq(Category::getId, categoryBrand.getCategoryId()));
            categories.add(category);
        }
        return ResponseEntity.ok(categories);

    }
    /**
     * 根据商品分类的id集合查询商品分类的名称
     * @param ids
     * @return
     */
    @RequestMapping("/names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){
        List<String> list = categoryService.queryNamesByIds(ids);
        return ResponseEntity.ok(list);
    }
}